<html>
<head>
  <title>max2.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#444444">#!/usr/bin/python</font>
<font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
Trivial Gather Example

One way to find the max of a large set of numbers by
divide and conquer.
&quot;</font><font color="#008000">&quot;&quot;</font>

<strong>import</strong> <font color="#2040a0">mpi</font>
<strong>import</strong> <font color="#2040a0">random</font>

<font color="#2040a0">problemlength</font> <font color="4444FF">=</font> <font color="#FF0000">1000</font>
    
<font color="#2040a0">def</font> <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <font color="#2040a0">myrank</font>, <font color="#2040a0">size</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">init</font><font color="4444FF">(</font><font color="4444FF">)</font>

    <font color="#444444"># split the problem in chunks</font>

    <strong>if</strong> <font color="#2040a0">problemlength</font> <font color="4444FF">%</font> <font color="#2040a0">size</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
        <font color="#2040a0">blocksize</font> <font color="4444FF">=</font> <font color="#2040a0">problemlength</font> / <font color="#2040a0">size</font>
    <strong>else</strong><font color="4444FF">:</font>
        <strong>print</strong> <font color="#008000">&quot;Sorry, I don't know how to split up the problem, aborting!&quot;</font>
        <font color="#2040a0">mpi</font>.<font color="#2040a0">finalize</font><font color="4444FF">(</font><font color="4444FF">)</font>
        
    <strong>if</strong> <font color="#2040a0">myrank</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
        <font color="#2040a0">data</font> <font color="4444FF">=</font> <font color="#2040a0">range</font><font color="4444FF">(</font><font color="#FF0000">1</font>,<font color="#2040a0">problemlength</font> <font color="4444FF">+</font> <font color="#FF0000">1</font><font color="4444FF">)</font>  <font color="#444444"># create a toy dataset...</font>
        <font color="#2040a0">random</font>.<font color="#2040a0">shuffle</font><font color="4444FF">(</font><font color="#2040a0">data</font><font color="4444FF">)</font>               <font color="#444444"># ...modifies data in place</font>

        <font color="#2040a0">mydata</font> <font color="4444FF">=</font> <font color="#2040a0">data</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">:</font><font color="#2040a0">blocksize</font><font color="4444FF">]</font> <font color="#444444"># get some data for me...</font>
                                   <font color="#444444"># and communicate the rest to slaves</font>

        <strong>for</strong> <font color="#2040a0">host</font> <strong>in</strong> <font color="#2040a0">range</font><font color="4444FF">(</font><font color="#FF0000">1</font>,<font color="#2040a0">size</font><font color="4444FF">)</font><font color="4444FF">:</font>
            <font color="#2040a0">hisdata</font> <font color="4444FF">=</font> <font color="#2040a0">data</font><font color="4444FF">[</font><font color="#2040a0">blocksize</font><font color="4444FF">*</font><font color="#2040a0">host</font><font color="4444FF">:</font><font color="#2040a0">blocksize</font><font color="4444FF">*</font><font color="4444FF">(</font><font color="#2040a0">host</font><font color="4444FF">+</font><font color="#FF0000">1</font><font color="4444FF">)</font><font color="4444FF">]</font>
            <font color="#2040a0">mpi</font>.<font color="#2040a0">send</font><font color="4444FF">(</font><font color="#2040a0">hisdata</font>,<font color="#2040a0">blocksize</font>,<font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_INT</font>,<font color="#2040a0">host</font>,<font color="#FF0000">0</font>,<font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font><font color="4444FF">)</font>
    <strong>else</strong><font color="4444FF">:</font>
        <font color="#2040a0">mydata</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">recv</font><font color="4444FF">(</font><font color="#2040a0">blocksize</font>,<font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_INT</font>,<font color="#FF0000">0</font>,<font color="#FF0000">0</font>,<font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font><font color="4444FF">)</font>

    <font color="#2040a0">mymax</font> <font color="4444FF">=</font> <font color="#2040a0">max</font><font color="4444FF">(</font><font color="#2040a0">mydata</font><font color="4444FF">)</font>

    <font color="#2040a0">maximums</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">gather</font><font color="4444FF">(</font><font color="#2040a0">mymax</font>,<font color="#FF0000">1</font>,<font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_INT</font>, <font color="#2040a0">size</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_INT</font>, <font color="#FF0000">0</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font><font color="4444FF">)</font>

    <strong>if</strong> <font color="#2040a0">myrank</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
        <font color="#2040a0">mymax</font> <font color="4444FF">=</font> <font color="#2040a0">max</font><font color="4444FF">(</font><font color="#2040a0">maximums</font><font color="4444FF">)</font>
        <strong>print</strong> <font color="#008000">&quot;The maximum value is:&quot;</font>, <font color="#2040a0">mymax</font>

    <font color="#2040a0">mpi</font>.<font color="#2040a0">finalize</font><font color="4444FF">(</font><font color="4444FF">)</font>            

<strong>if</strong> <font color="#2040a0">__name__</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
    <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font>


</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
